<?php
function view_bckuser($config,$param) {
    $db = new db($config);
    $result = $db->query("SELECT * FROM user WHERE role <> 1");
    $param['pengguna'] = true;
    $param['loggedin'] = true;
    view_backend_header($config, $param);
    ?>
    <h3>Edit Pengguna</h3>
    <?php
    //verifikasi isian gunakan javascript
    if (isset($_SESSION['editted'])) {
        echo '<div class="alert alert-warning">'.$_SESSION['editted'].'</div>';
        unset($_SESSION['editted']);
    }
    ?>
	<script type="text/javascript">
		function validateEmpty(fld) {
			var error = "";
		 
			if (fld.value.length == 0) {
				fld.style.background = 'Yellow'; 
				error = "1";
			} else {
				fld.style.background = 'White';
			}
			return error;  
		}
		function validateEdit(theForm) {
			var reason = "";
			
			  reason += validateEmpty(theForm.unameed);
			  reason += validateEmpty(theForm.newpass);
			  reason += validateEmpty(theForm.olpass);
				  
			  if (reason != "") {
				alert("Perhatian, isi yang masih kosong.");
				return false;
			  }

			  return true;
		}
		function validateAdd(theForm) {
			var reason = "";
			
			  reason += validateEmpty(theForm.uname);
			  reason += validateEmpty(theForm.pass);
				  
			  if (reason != "") {
				alert("Perhatian, isi yang masih kosong.");
				return false;
			  }

			  return true;
		}
	</script> 
    <div>
        <form method="post" action="<?php echo $config['home_url']."backend/eduser"?>" onsubmit="return validateEdit(this)">
			<input type="hidden" name="id" value="<?php echo $_SESSION['logged']['idUser']; ?>" />
            <div>Username : <input type="text" name="unameed" value="<?php echo $_SESSION['logged']['username']; ?>" /></div>
            <div>New Password : <input type="password" name="newpass" autocomplete="off" /></div>
            <div>Old Password : <input type="password" name="olpass" autocomplete="off" /></div>
            <div><input class="btn" type="submit" value="Ubah" name="ubah" /></div>
        </form>
    </div>
	<?php
	if ($_SESSION['logged']['role']==1){
		echo "<h3 onclick=\"javascript:showHide('users');\">Pengguna++</h3>";
		echo "<div id=\"users\" style=\"display:none\">";
			echo "<div>";
				echo "<form method=\"post\" action=\"".$config['home_url']."backend/adduser\" onsubmit=\"return validateAdd(this)\">";
					echo "<div>Username : <input type=\"text\" name=\"uname\" autocomplete=\"off\" /></div>";
					echo "<div>Password : <input type=\"password\" name=\"pass\" autocomplete=\"off\" /></div>";
					echo "<div><input class=\"btn\" type=\"submit\" value=\"Tambah\" name=\"tambah\" /></div>";
				echo "</form>";
			echo "</div>";
			echo "<ul>";
			foreach ($result as $res){
				echo "<li>".$res['username']." <a href=\"".$config['home_url']."backend/deluser?id=".$res['idUser']."\">(X)</a></li>";
			}
			echo "</ul>";
		echo "</div>";
	}
	?>
    <?php
    view_backend_footer($config, $param);
}
